// pages/me/dealerUserApply/dealerUserApply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyList:[],
    ScopeUser:{
      applyId:null,
        status:1,
        userTitle:'',
        userMobile:'',
        userName:'',
        roleList:[],
      },
      roleList:[
          {value:'viewCar',name:'查看车辆'},
          {value:'addCar',name:'发布车辆'},
          {value:'editCar',name:'编辑车辆'},
          {value:'closCar',name:'下架车辆'},
          {value:'manageWantBuy',name:'管理求购'},
          {value:'manageAuction',name:'管理拍卖'},
          {value:'manageSecondBatch',name:'管理一口价'},
          {value:'contactCustomer',name:'联系客户'},
          {value:'manageDealer',name:'管理公司'},
          {value:'manageTeam',name:'管理团队'}
      ],
      dfRole:[{
        name:'展厅经理',
        value:['viewCar','addCar','editCar','closCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer','manageDealer','manageTeam']
      },{
        name:'内勤专员',
        value:['viewCar','addCar','editCar','closCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer']
      },{
        name:'收车顾问',
        value:['viewCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer']
      },{
        name:'金牌销售',
        value:['viewCar','addCar','editCar','closCar','manageAuction','manageSecondBatch','contactCustomer']
      },],
      dfRoleIndex:null,
      applyIndex:0,
      saveSuccess:false,
      OKsave: false,
      modalName:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(app.globalData.ScopeUser == null){
      app.showNoAuthOrNoGrant('缺少必要参数,请重试');
    }else{
      this.InitUserStatus();
    }
  },
  // 检查一下登录状态。
  async InitUserStatus(){
      app.showLoading("获取用户信息");
      await app.checkToken();
      this.InitUserInfo();
      wx.hideLoading();
  },
  InitUserInfo(){
      let userInfo = app.globalData.userInfo;
      // 这里再验证一遍权限。
      if (userInfo.roleList.indexOf('manageTeam') == -1) {
          app.showNoAuthOrNoGrant('您无权操作查看此页面');
          return;
      }else{
        this.setData({
          'ScopeUser': app.globalData.ScopeUser
        })
      }
  },
  dealerUserAuditApply(e){
      let user = app.deepCopy(this.data.ScopeUser);
      user.auditStatus = e.currentTarget.dataset.type;
      if(e.currentTarget.dataset.type == 1){
        if(user.userName == ''){
          app.alert('请输入用户姓名');
          return;
        }
        if(user.userTitle == ''){
          app.alert('请输入职务名称');
          return;
        }
        if(user.roleList.length == 0){
          app.alert('请选择用户权限');
          return;
        }
      }
      let self = this;
      delete user.roleObj;
      app.sHttp.dealerUserAuditApply(user).then(res =>{
        wx.showModal({
          title: '提醒',
          content: '处理完毕',
          confirmText: '确定',
          showCancel: false,
          success(res) {
            self.BackPage();
          }
        })
      },err=>{
        app.error(err.msg);
      })
  },
  inputZw(e){
      let index = e.currentTarget.dataset.idx;
      let applyList = this.data.applyList;
      applyList[index].userTitle = e.detail.value;
      this.setData({
          'applyList': applyList
      })
  },
  publishInput(e){
    let ScopeUser = this.data.ScopeUser;
    ScopeUser[e.currentTarget.dataset.name] = e.detail.value;
    // this.setData({
    //   'ScopeUser':ScopeUser
    // });
    if(e.currentTarget.dataset.name == 'userTitle'){
      if(null != this.data.dfRoleIndex){
        this.setData({
          'ScopeUser':ScopeUser,'dfRoleIndex':null
        });
      }else{
        this.setData({
          'ScopeUser':ScopeUser
        });
      }
    }else{
      this.setData({
        'ScopeUser':ScopeUser
      });
    }
  },
  clickdfRole(e){
    let index = e.currentTarget.dataset.idx;
    let roleList = this.data.dfRole[index];
    if(this.changeUserRole({detail:{value:roleList.value}})){
      this.setData({
        'dfRoleIndex': index,
        'ScopeUser.userTitle':roleList.name
      })
    }
  },
  closeDfRole(e){
    var thisTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    if (lastTime != 0) {
      if (thisTime - this.data.lastTime < 500){
        let roleList = [];
        this.changeUserRole({detail:{value:roleList}});
      }
    }
    this.setData({
      lastTime: thisTime
    })
  },
  changeUserRole(e){
      let roleList = e.detail.value;
      let ScopeUser = app.deepCopy(this.data.ScopeUser);
      ScopeUser.roleList = roleList;
      ScopeUser.roleObj = {'viewCar':0,'addCar':0,'editCar':0,'closCar':0,'manageWantBuy':0,'manageAuction':0,'manageSecondBatch':0,'contactCustomer':0,'manageDealer':0,'manageTeam':0};
      roleList.forEach(item =>{
        ScopeUser.roleObj[item] = 1
      })
      this.setData({
          'ScopeUser': ScopeUser,
          'dfRoleIndex': null
      });
      return true
  },
  // changeScopeData(e){
  //   let index = e.currentTarget.dataset.idx;
  //   this.setData({
  //     'ScopeUser':{
  //       applyId:null,
  //       status:1,
  //       userTitle:'',
  //       userMobile:'',
  //       userName:'',
  //       roleList:[],
  //     }
  //   });
  //   let appLust = this.data.applyList;
  //   let item = app.deepCopy(appLust[index]);
  //   this.setData({
  //     'ScopeUser.applyId':item.applyId,
  //     'ScopeUser.userMobile':item.mobile,
  //     'ScopeUser.userName':item.userName,
  //     'applyIndex':index
  //   });
    
  // },
// //   去处理按钮
//   goOptions(){
//     let appLust = this.data.applyList;
//     appLust.splice(this.data.applyIndex,1);
//     this.setData({
//       'applyList':appLust,
//       'applyIndex':0,
//       'modalName':null,
//       'OKsave':false,
//       'saveSuccess':false
//     })

//   },
  BackPage() {
    var that = this;
    wx.navigateBack({
      delta: 1,
      fail: function(){
        that.toHome();
      }
    });
  },
  toHome(){
    wx.reLaunch({
      url: '/pages/home/home',
    })
  }
})